package com.tdt.alarm.controller;

import java.util.Random;

import android.app.Activity;
import android.app.Notification;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.NotificationCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.tdt.alarm.R;
import com.tdt.alarm.model.SettingDBHelper;
import com.tdt.alarm.model.SettingModel;

public class AlarmAction extends Activity {
	Button btnDismiss;
	Button btnChangePos;
	RatingBar ratingBar;
	CountDownTimer cdt;
	TextView countDownStatus;
	Ringtone r;
	MediaPlayer mp;
	SettingModel setting;
	SettingDBHelper settingDB;
	LinearLayout headLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alarm_action);

		btnDismiss = (Button) findViewById(R.id.btnDismiss);
		btnChangePos = (Button) findViewById(R.id.btnChangePos);
		ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
		countDownStatus = (TextView) findViewById(R.id.countDownStatus);
		
		headLayout = (LinearLayout) findViewById(R.id.linearLayoutHead);
		
		ratingBar.setEnabled(false);
		
		cdt = new CountDownTimer(1000, 1000) {
			
			@Override
			public void onTick(long millisUntilFinished) {
				//countDownStatus.setText("Button will run away after "+millisUntilFinished/1000 +"s");
				
			}
			
			@Override
			public void onFinish() {
				changeButtonPosition();
			}
		};

		cdt.start();
		btnChangePos.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeButtonPosition();
			}
		});
		
		// set visible to test
		btnChangePos.setVisibility(View.INVISIBLE);
		
		btnDismiss.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int numStars = (int) (ratingBar.getRating());
				
				if(numStars < 4){
					mp = MediaPlayer.create(getApplicationContext(), R.drawable.correct_answer);
					mp.start();
					numStars++;
					ratingBar.setRating(numStars);
					changeButtonPosition();
				} else {
					mp = MediaPlayer.create(getApplicationContext(), R.drawable.complete_dismiss);
					mp.start();
					Intent intent = new Intent(getApplicationContext(), MainActivity.class);
					cdt.cancel();
					r.stop();
					finish();
					startActivity(intent);
				}
				
			}
		});
		
		
		/*
		 * play ringtone
		 */
		settingDB = new SettingDBHelper(getApplicationContext());
		setting = settingDB.getFirstSetting();
		
//		Uri notification = RingtoneManager
//				.getDefaultUri(RingtoneManager.TYPE_ALL);
//		String posString = Utils.readRingToneUri();
//		int pos = 1;
//		if(posString != null){
//			pos = Integer.parseInt(posString);
//		}
		RingtoneManager mRingtoneManager = new RingtoneManager(getApplicationContext());
		mRingtoneManager.setType(RingtoneManager.TYPE_RINGTONE);

	    int length = mRingtoneManager.getCursor().getCount();
		Uri uri = mRingtoneManager.getRingtoneUri(0);
		
		if (setting != null){
			uri = setting.getRingtone();
		}
		r = RingtoneManager.getRingtone(getApplicationContext(), uri);
		//r = RingtoneManager.getRingtone(getApplicationContext(), notification);

		if (!r.isPlaying()) {
			r.play();
		}
		NotificationCompat.Builder mBuilder =
		        new NotificationCompat.Builder(getApplicationContext())
		                .setSmallIcon(R.drawable.scrubber_control_pressed_holo)
		                .setContentTitle("test")
		                .setContentText("some text")
		                .setDefaults(Notification.DEFAULT_ALL)
		                .setAutoCancel(true);
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/*
	 * change Dismiss button position
	 */
	private void changeButtonPosition() {
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		
		//Screen Resolution x->width y->height
		int height = displaymetrics.heightPixels;
		int width = displaymetrics.widthPixels;
		
		Log.i("Screen Height", height + "");
		Log.i("Screen Width", width + "");
		
		//Get end of head position
		int headX = (int) headLayout.getX() + headLayout.getWidth();
		int headY = (int) headLayout.getY() + headLayout.getHeight();
		
		Log.i("Head end pos X", headX + "");
		Log.i("Head end pos Y", headY + "");
		
		//Get button size
		int buttonH = btnDismiss.getHeight();
		int buttonW = btnDismiss.getWidth();
		
		//Get button running area size
		int heightRA = height - headY - 2 * buttonH;
		int widthRA =  width - buttonW;
		
		Random rand = new Random();
		int xPosition = rand.nextInt(widthRA);
		int yPosition = rand.nextInt(heightRA) + headY;
		
		@SuppressWarnings("deprecation")
		AbsoluteLayout.LayoutParams absParams = (AbsoluteLayout.LayoutParams) btnDismiss
				.getLayoutParams();
		absParams.x = xPosition;
		absParams.y = yPosition;
		btnDismiss.setLayoutParams(absParams);
		
		Log.i("Button current pos X", btnDismiss.getX() + "");
		Log.i("Button current pos Y", btnDismiss.getY() + "");
		cdt.cancel();
		cdt.start();
		
//		btnChangePos.setText(String.valueOf(xPosition)+ " x " + String.valueOf(yPosition));
//		btnDismiss.setText(String.valueOf(width)+ " x " + String.valueOf(height));
	}
	
	// không cho nhấn nút back
	@Override
	public void onBackPressed() {
		Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
	}
}
